package com.czxy.controller;

import com.czxy.domain.CertificateType;
import com.czxy.domain.User;
import com.czxy.domain.UserVo;
import com.czxy.service.UserService;
import com.czxy.vo.BaseResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangmengxiang1@itcast.cn
 * @version 1.0
 * @date 2020/1/30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    //注册功能
    // 1.1 添加用户
    @PostMapping("/addUser")
    public BaseResult addUser(@RequestBody User user){
        System.out.println(user);
        try {
            userService.addUser(user);
        } catch (Exception e) {
            return BaseResult.error("注册失败");
        }
        return BaseResult.ok("注册成功");
    }
    //1.2 查询出所有证件类型
    @GetMapping("/findAllidtype")
    public BaseResult<List<CertificateType>> findAllidType(){
        List<CertificateType> certificateTypeList = null;
        try {
            certificateTypeList = userService.findAllidType();
        } catch (Exception e) {
            return BaseResult.error("查询失败");
        }
        return BaseResult.ok("查询成功",certificateTypeList);
    }

    //用户登录
    @PostMapping("/login")
    public BaseResult login(@RequestBody User user){
        System.out.println(user);
        User user1 = userService.login(user);
        if (user1 == null){
            return BaseResult.error("用户名或密码错误");
        }
        return BaseResult.ok("登录成功",user1);
    }

    //查询所有的用户 + 分页
    @PostMapping("/findAllUserForpage")
    public BaseResult<PageInfo<User>> findAllUserForpage(@RequestBody UserVo userVo){
        PageInfo<User> pageInfo = userService.findAllUserForpage(userVo);
        return BaseResult.ok("查询成功",pageInfo);
    }

    //修改1 根据id查询出用户
    @GetMapping("/findOneUserByuid/{uid}")
    public BaseResult findOneUserByuid(@PathVariable("uid") Integer uid){
        User user = userService.findOneUserByuid(uid);
        return BaseResult.ok("查询成功",user);
    }

    //修改 用户
    @PutMapping("/updateuser")
    public BaseResult updateuser(@RequestBody User user){
        userService.updateuser(user);
        return BaseResult.ok("修改成功");
    }

    //删除 用户
    @DeleteMapping("/delUserByuid/{uid}")
    public BaseResult delUserByuid(@PathVariable("uid") Integer uid){
        userService.delUserByuid(uid);
        return BaseResult.ok("删除成功");
    }

    //多个删除 用户
    @DeleteMapping("/delUserByStr/{dir}")
    public BaseResult delUserByStr(@PathVariable("dir") String dir){
        userService.delUserByStr(dir);
        return BaseResult.ok("批量删除成功");
    }


}
